<template>
  <el-dialog
    :title="'个人钱包记录'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    v-el-drag-dialog
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="用户id" prop="id" label-width="100px">
        <el-input v-model="dataForm.id" size="12px"></el-input>
      </el-form-item>
      <el-form-item label="可用余额" prop="balance" label-width="100px">
        <el-input v-model="dataForm.balance" size="12px"></el-input>
      </el-form-item>
      <el-form-item label="积分" prop="point" label-width="100px">
        <el-input v-model="dataForm.point" size="12px"></el-input>
      </el-form-item>
      <el-form-item label="冻结积分" prop="frozenPoint" label-width="100px">
        <el-input v-model="dataForm.frozenPoint" placeholder></el-input>
      </el-form-item>
      <el-form-item label="冻结可用余额" prop="frozenBalance" label-width="100px">
        <el-input v-model="dataForm.frozenBalance" size="12px"></el-input>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id:"", // 用户id
        balance: "", // 可用余额
        point: "", // 可用余额
        frozenPoint: "", // 冻结积分
        frozenBalance: "" // 冻结可用余额
      },
      dataRule: {
        paramKey: [
          { required: true, message: "参数名不能为空", trigger: "blur" }
        ],
        paramValue: [
          { required: true, message: "参数值不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/productAgree/list/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.status === 200) {
              for (var i = 0; i < data.data.list.length; i++) {
                this.dataForm.id = data.data.list[i].id;
                this.dataForm.point = data.data.list[i].point;
                this.dataForm.balance = data.data.list[i].balance;
                this.dataForm.frozenPoint = data.data.list[i].frozenPoint;
                this.dataForm.frozenBalance = data.data.list[i].frozenBalance;
              }
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/memberWallet/operate/`),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              flag: this.dataForm.flag,
              num: this.dataForm.num,
              type: this.dataForm.type,
              userId: this.dataForm.userId
            })
          }).then(({ data }) => {
            if (data && data.status === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.message);
            }
          });
        }
      });
    }
  }
};
</script>
